package com.example.administrator.printoutindemo.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.adapter.OutStoreAdapter;
import com.example.administrator.printoutindemo.bean.OutKuListBean;
import com.example.administrator.printoutindemo.di.component.DaggerChuKuComponent;
import com.example.administrator.printoutindemo.di.module.ChuKuModule;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.example.administrator.printoutindemo.mvp.contract.ChuKuContract;
import com.example.administrator.printoutindemo.mvp.presenter.ChuKuPresenter;
import com.example.administrator.printoutindemo.mvp.ui.entity.ChuKu;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_CHU_KU)
public class ChuKuActivity extends BaseActivity<ChuKuPresenter> implements ChuKuContract.View, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.empty)
    View mEmptyLayout;
    @BindView(R.id.empty_text)
    TextView empty_text;
    @BindView(R.id.empty_image)
    ImageView empty_image;
    @BindView(R.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    private ListView lv_cklb;
    private ImageView tv_chaxunck;
    private EditText et_phone;
    private EditText et_dan_hao;
    private List<OutKuListBean> mlist = new ArrayList<>();
    private OutStoreAdapter mAdapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerChuKuComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .chuKuModule(new ChuKuModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_chu_ku; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_chu_ku));
//        getChuKuData();
        initView1();
        initEmpty();
        initRefreshLayout();
        getSearchOutStorageList();
    }

    private void initView1() {
        lv_cklb = findViewById(R.id.lv_cklb);
        tv_chaxunck = findViewById(R.id.tv_chaxunck);
        et_phone = findViewById(R.id.et_phone);
        et_dan_hao = findViewById(R.id.et_dan_hao);
        mAdapter = new OutStoreAdapter(getContext(), mlist);
        lv_cklb.setAdapter(mAdapter);
        tv_chaxunck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv_cklb.setVisibility(View.GONE);
                mEmptyLayout.setVisibility(View.VISIBLE);
                mAdapter.clearData();
                mStart = 1;
                getSearchOutStorageList();
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(com.xiaoxing.zhinenghuacangku.R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#CFCFCF"));
        empty_text.setText("暂无数据");
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(com.xiaoxing.zhinenghuacangku.R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadMore(true);
    }

    private void getSearchOutStorageList() {

        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);

        String methodName = "SearchOutStorageList0315Paging";

        SoapParams params = new SoapParams();
        params.put("userPhone", et_phone.getText().toString().trim());
        params.put("uid", SPUtil.getString(getContext(), "iduser", ""));
        params.put("batchid", et_dan_hao.getText().toString().trim());
        params.put("startIndex", String.valueOf(mStart));
        params.put("endIndex", String.valueOf(mStart + mNum - 1));


        LogUtils.debugEInfo("startIndex=" + String.valueOf(mStart));
        LogUtils.debugEInfo("endIndex=" + String.valueOf(mStart + mNum - 1));
        /**
         * 请求
         */
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mAdapter.shua();
                    List<OutKuListBean> outKuListBeen = new Gson().fromJson(result, new TypeToken<List<OutKuListBean>>() {
                    }.getType());
                    if (outKuListBeen != null && outKuListBeen.size() > 0) {
                        mStart += outKuListBeen.size();
                        mEmptyLayout.setVisibility(View.GONE);
                        lv_cklb.setVisibility(View.VISIBLE);
                        mAdapter.addAll(outKuListBeen);
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();

            }
        }, getAapiUserName(), getApiPwd());

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        getSearchOutStorageList();
        mRefreshLayout.finishLoadMore();
    }

    private void getChuKuData() {
        mPresenter.getChuKuData();
    }

    @Override
    public void getChuKuDataSuccess(ChuKu entityData) {


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
